package com.kongfuzi.springaop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Copyright (C), 2018-2020
 * FileName: LogAspect
 * 日志切面
 */
@Component
@Aspect
public class LogAspect {
    @Pointcut("execution(* com.kongfuzi.springaop.*.*(..))")
    private void mypointCut(){}//标识方法

    @Before(value = "mypointCut()")
    public void before(JoinPoint jp){
        System.out.println("前置通知(切入点表达式为):"+jp);
        System.out.println("前置通知(方法签名为):"+jp.getSignature());
        System.out.println("前置通知(目标对象为):"+jp.getTarget());

        Object[] args = jp.getArgs();
        if (args.length != 0){
            System.out.println("前置通知(方法参数为):");
            for (Object o :args){
                System.out.println(o + " ");
            }
        }
    }
    @AfterReturning(value = "mypointCut()",returning = "result")
    public void afterReturning(Object result){
        System.out.println("后置通知:目标方法返回执行结果为:"+result);
    }

    @AfterThrowing(value = "mypointCut()",throwing = "e")
    public void afterThrowing(Throwable e){
        System.out.println("异常通知:"+e.getMessage());
    }

    @Around("mypointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable{
        System.out.println("环绕通知: 前.........");
        Object result = pjp.proceed();
        System.out.println("环绕通知: 后.........执行结果:"+result);
        return result;
    }

    @After(value = "mypointCut()")
    public void after(){
        System.out.println("执行最终通知.....");
    }

}
